<template>
  <div>
    <el-card class="!border-none relative el-card-client" shadow="never">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="5">
            <el-input
              v-model="queryParams.name"
              class="input_style"
              clearable
              placeholder="请输入搜索关键字"
            />
          </el-col>
          <el-col :span="5">
            <el-select
              class="input_style"
              v-model="queryParams.ord"
              clearable
              placeholder="请选择行业"
            >
              <el-option
                v-for="(item, index) in options"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-col>
          <el-col :span="2">
            <el-checkbox
              class="input style input_time_style"
              v-model="queryParams.is_report"
              border
              label="申报"
            />
          </el-col>
          <el-col :span="10">
            <el-button
              type="primary btn_style"
              @click="resetPage"
              color="#63ccba"
              class="mr_5"
            >
              <template #icon>
                <el-icon color="#fff">
                  <Search />
                </el-icon> </template
              >搜索
            </el-button>
            <el-button @click="resetParams" class="mr_5">重置</el-button>
            <el-button
              v-perms="['activity.zg_apply/add']"
              type="primary"
              @click="handleAdd"
            >
              <template #icon>
                <icon name="el-icon-Plus" />
              </template>
              新增选手
            </el-button>
            <!-- <el-button @click="resetParams" v-show="selectData.length" class="mr_5" type="danger">
              <template #icon>
                <el-icon>
                  <Connection />
                </el-icon>
              </template>批量拉黑
            </el-button>
            <el-button @click="resetParams" v-show="selectData.length" class="mr_5" type="primary">
              <template #icon>
                <el-icon>
                  <Connection />
                </el-icon>
              </template>批量拉白
            </el-button>-->
          </el-col>
          <!-- <el-col :span="2" class="flex justify-end">
            <move-button></move-button>
          </el-col> -->
        </el-row>
      </template>
      <div>
        <p class="title text_center fz_16 font600 pd_t_b">{{ title }}</p>
        <el-table
          :data="pager.lists"
          border
          @selection-change="handleSelectionChange"
          v-loading="pager.loading"
          stripe
          height="calc(100vh - 18rem)"
        >
          <!-- <el-table-column label="序号" prop="id" align="center" width="60" /> -->
          <el-table-column label="选手" prop="name" align="center" />
          <el-table-column label="数据">
            <template #default="{ row }">
              <div class="demo_cointainer flex">
                <div class="width_half">
                  <p>
                    总票数：
                    <span class="fz_16 red">{{ row.votes }}</span>
                  </p>
                  <p class="font600">
                    助力：
                    <span class="red fz_16">{{ row.or_num }}</span>
                  </p>
                </div>
                <div class="width_half">
                  <p>
                    浏览量：
                    <span class="fz_16 blue">{{ row.visits }}</span>
                  </p>
                  <p class>
                    热度：
                    <span class="red fz_16">{{ row.hot }}</span>
                  </p>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            prop="remark"
            align="center"
            width="150"
          >
            <template #default="{ row }">
              <el-button type="info" link @click="openRemark(row)">{{
                row.remark ? row.remark : "未编辑"
              }}</el-button>
            </template>
          </el-table-column>
          <!-- <el-table-column label="添加时间" prop="create_time" align="center" width="150" /> -->
          <el-table-column label="操作" prop="remark" align="center" width="60">
            <template #default="{ row }">
              <el-dropdown style="width: 160px" trigger="click">
                <span class="el-dropdown-link">
                  <el-icon class="fz_16 font600">
                    <More />
                  </el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item class="mt_10 mb_10">
                      <el-link :underline="false" @click="handleEdit(row)"
                        >编辑选手</el-link
                      >
                    </el-dropdown-item>
                    <el-dropdown-item class="mt_10 mb_10">
                      <el-link
                        type="success"
                        :underline="false"
                        @click="cpoyLink(row.link)"
                        >复制链接</el-link
                      >
                    </el-dropdown-item>
                    <el-dropdown-item class="mt_10 mb_10">
                      <el-link
                        type="primary"
                        :underline="false"
                        @click="leaveManage(row)"
                        >选手日志</el-link
                      >
                    </el-dropdown-item>
                    <el-dropdown-item class="mt_10 mb_10">
                      <el-link
                        type="primary"
                        :underline="false"
                        @click="leaveVotes(row)"
                        >选手评论</el-link
                      >
                    </el-dropdown-item>
                    <!-- <el-dropdown-item class="mt_10 mb_10">
                      <el-link type="primary" :underline="false" @click="leaveApply(row)">选手审核</el-link>
                    </el-dropdown-item>-->
                    <el-dropdown-item class="mt_10 mb_10">
                      <el-link
                        type="primary"
                        :underline="false"
                        @click="leaveGiftLog(row)"
                        >助力日志</el-link
                      >
                    </el-dropdown-item>
                    <el-dropdown-item
                      class="mt_10 mb_10 text_center flexCenter"
                    >
                      <el-link
                        type="danger"
                        :underline="false"
                        @click="handleDelete(row.id)"
                        >删除</el-link
                      >
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="position">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <el-dialog v-model="showRemark" title="编辑备注" width="30%" center>
      <el-form ref="formRef" :model="formData" label-width="50px">
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="formData.remark"
            :rows="5"
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showRemark = false">取消</el-button>
          <el-button type="primary" @click="submitRemark">确定</el-button>
        </span>
      </template>
    </el-dialog>
    <edit-popup
      v-if="showEdit"
      ref="editRef"
      :group-id="groupId"
      @success="getLists"
      @close="showEdit = false"
    />
    <QR-code-url
      :QrCodeVisible="QrCodeVisible"
      :QrCodeUrl="QrCodeUrl"
      @close="QrCodeVisible = false"
    ></QR-code-url>
  </div>
</template>

<script lang="ts" setup name="zgVotesLogLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import {
  apiPlayerLists,
  apiZgPlayerDetail,
  apiZgPlayerDelete,
  apiZgPlayerRemark,
} from "@/api/zg_player_list";
import { timeFormat } from "@/utils/util";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";
import moveButton from "@/components/moveButton/index.vue";
import QRCodeUrl from "@/components/QRCodeUrl/index.vue";
import { ElMessage } from "element-plus";
import { getRoutePath } from "@/router";
import useClipboard from "vue-clipboard3";
const router = useRouter();
const route = useRoute();
const title = route.query.title;
const groupId = route.query.id;
const options = reactive([
  {
    name: "编号正序",
    value: "1",
  },
  {
    name: "编号倒序",
    value: "2",
  },
  {
    name: "票数正序",
    value: "3",
  },
  {
    name: "票数倒序",
    value: "4",
  },
]);
// 查询条件
const queryParams = reactive({
  ord: "",
  is_report: "",
  activity_id: route.query.id,
  name: "",
});

// 选中数据
const selectData = ref<any[]>([]);

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({ id }) => id);
};

const editRef = shallowRef<InstanceType<typeof EditPopup>>();
// 是否显示编辑框
const showEdit = ref(false);

// 获取字典数据
const { dictData } = useDictData("");

//编辑备注
const formData = reactive({
  id: "",
  remark: "",
});
// 是否显示备注编辑框
const showRemark = ref(false);
const submitRemark = async () => {
  await apiZgPlayerRemark(formData);
  formData.remark = "";
  showRemark.value = false;
  getLists();
};
const openRemark = async (data: any) => {
  showRemark.value = true;
  formData.id = data.id;
  formData.remark = data.remark;
};

//  复制链接 // 二维码
const { toClipboard } = useClipboard();
const QrCodeVisible = ref(false);
const QrCodeUrl = ref("");
const cpoyLink = (link: string) => {
  QrCodeVisible.value = true;
  QrCodeUrl.value = link;
  return;
  navigator.clipboard
    .writeText(link)
    .then(() => {
      ElMessage({
        message: "已成功复制到剪切板",
        type: "success",
      });
    })
    .catch((err) => {
      // 复制失败
      ElMessage.error(err);
    });
};

//  路由跳转
const leaveManage = (item: any) => {
  router.push({
    path: getRoutePath("activity.zg_player_log/lists"),
    query: {
      id: item.id,
      topTitle: title,
      name: item.name,
      images: item.image,
    },
  });
};
const leaveVotes = (item: any) => {
  router.push({
    path: getRoutePath("activity.zg_player_comment/lists"),
    query: {
      id: item.id,
      title: item.title,
    },
  });
};
const leaveApply = (item: any) => {
  router.push({
    path: getRoutePath("activity.zg_apply/lists"),
    query: {
      id: item.id,
      title: item.title,
    },
  });
};
const leaveGiftLog = (item: any) => {
  router.push({
    path: getRoutePath("activity.zg_gift_log/lists"),
    query: {
      id: item.id,
      topTitle: title,
      name: item.name,
      images: item.image,
    },
  });
};

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: apiPlayerLists,
  params: queryParams,
});

// 添加
const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  const res = await apiZgPlayerDetail({ id: data.id });
  editRef.value?.open("edit");
  editRef.value?.setFormData(res);
};

// 删除
const handleDelete = async (id: number) => {
  await feedback.confirm(`已选中名称为“${id}”的选手`);
  await apiZgPlayerDelete({ id });
  getLists();
};

getLists();
</script>
<style lang="scss" scoped>
.flex {
  display: flex !important;
}
.input_style {
  width: 100%;
}
.align_center {
  align-content: center;
  align-items: center;
  justify-content: left !important;
}
.demo_cointainer {
  .width_half {
    width: 50%;
  }
}
</style>
